package com.example.lession71.slice;

import com.example.lession71.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;

public class ComListenerAbilitySlice extends AbilitySlice {


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_com_listener);
        Button btn4 = (Button) findComponentById(ResourceTable.Id_btn4);
        Button btn5 = (Button) findComponentById(ResourceTable.Id_btn5);
        Button btn6 = (Button) findComponentById(ResourceTable.Id_btn6);
        Text text = (Text) findComponentById(ResourceTable.Id_show);
        //3 创建组件的事件监听器 点击事件监听器
        // js function xxx(){}
        Component.ClickedListener clickedListener = new Component.ClickedListener() {

            @Override
            public void onClick(Component component) {
                ToastDialog dialog = new ToastDialog(getContext());
                String msg = "";
                if (component==btn4) {
                    msg="触发了4号按钮";
                }else if (component==btn5) {
                    msg="触发了5号按钮";
                }if (component==btn6) {
                    msg="触发了6号按钮";
                }
                dialog.setText(msg);
                dialog.setDuration(5000);
                dialog.show();
                text.setText(msg);
            }
        };
//        Component.ClickedListener clickedListener = this::onClick;
        // 按钮和监听事件进行绑定
        btn4.setClickedListener(clickedListener);
        btn5.setClickedListener(clickedListener);
        btn6.setClickedListener(clickedListener);
    }



    Button btn4 = null;
    Button btn5 = null;
    Button btn6 = null;
    /**
     * 演示 可以使用lamdba 方法引用
     * @param component
     */
    private void onClick(Component component) {
        ToastDialog dialog = new ToastDialog(getContext());
        String msg = "";
        if (component == btn4) {
            msg = "触发了4号按钮";
        } else if (component == btn5) {
            msg = "触发了5号按钮";
        }
        if (component == btn6) {
            msg = "触发了6号按钮";
        }
        dialog.setText(msg);
        dialog.setDuration(5000);
        dialog.show();
    }


}
